package com.weed.table.head;

import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Dimension;
import javax.swing.plaf.basic.BasicTableHeaderUI;
import javax.swing.JComponent;
import javax.swing.table.TableCellRenderer;
import java.awt.Component;
import java.util.Hashtable;
import java.util.Enumeration;
import javax.swing.table.TableColumn;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumnModel;
import javax.swing.JTable;
import javax.swing.table.JTableHeader;
import javax.swing.UIManager;
import javax.swing.event.MouseInputListener;
import java.awt.event.MouseEvent;
import javax.swing.SwingUtilities;
import java.awt.Point;
/**
 * <p>Title: Swing扩展<</p>
 * <p>Description: JTableHeaderExUI是对BasicTableHeaderUI的扩展，实现了表头样式的扩展显示。</p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: FZProlink</p>
 * @author Jason
 * @version 1.0
 */

public class JTableHeaderExUI extends BasicTableHeaderUI{
  public JTableHeaderExUI() {
  }
  public void paint(Graphics g, JComponent c) {
    Rectangle clipBounds = g.getClipBounds();
    if (header.getColumnModel() == null) return;
    ((JTableHeaderEx)header).setColumnMargin(); 
    int column = 0;
    Dimension size = header.getSize();
    Rectangle cellRect  = new Rectangle(0, 0, size.width, size.height);
    Hashtable h = new Hashtable();
    int columnMargin = header.getColumnModel().getColumnMargin();
    Enumeration enumeration = header.getColumnModel().getColumns();
    while (enumeration.hasMoreElements()) {
      cellRect.height = size.height;
      cellRect.y      = 0;
      TableColumn aColumn = (TableColumn)enumeration.nextElement();
      Enumeration cGroups = ((JTableHeaderEx)header).getColumnGroups(aColumn);
      if (cGroups != null) {
        int groupHeight = 0;
        while (cGroups.hasMoreElements()) {
          ColumnGroup cGroup = (ColumnGroup)cGroups.nextElement();
          Rectangle groupRect = (Rectangle)h.get(cGroup);
          if (groupRect == null) {
            groupRect = new Rectangle(cellRect);
            Dimension d = cGroup.getSize(header.getTable());
            groupRect.width  = d.width;
            groupRect.height = d.height;
            h.put(cGroup, groupRect);
          }
          paintCell(g, groupRect, cGroup);
          groupHeight += groupRect.height;
          cellRect.height = size.height - groupHeight;
          cellRect.y      = groupHeight;
        }
      }
      cellRect.width = aColumn.getWidth() ;//+ columnMargin;
      if (cellRect.intersects(clipBounds)) {
        paintCell(g, cellRect, column);
      }
      cellRect.x += cellRect.width;
      column++;
    }



  }
  /**
   * 在Graphics上画出列的形状�?
   * @param g Graphics�?
   * @param cellRect  大小及位置�?
   * @param columnIndex 列的索引�?
   */
  private void paintCell(Graphics g, Rectangle cellRect, int columnIndex) {
    TableColumn aColumn = header.getColumnModel().getColumn(columnIndex);
    TableCellRenderer renderer = aColumn.getHeaderRenderer();
      
    if(renderer==null)
      renderer = new DefaultTableCellRenderer() {
       public Component getTableCellRendererComponent(JTable table,
           Object value,
           boolean isSelected, boolean hasFocus, int row, int column) {
         JTableHeader header = table.getTableHeader();
         if (header != null) {
           setForeground(header.getForeground());
           setBackground(header.getBackground());
           setFont(header.getFont());
         }
         setText( (value == null) ? "" : value.toString());
         setBorder(UIManager.getBorder("TableHeader.cellBorder"));
         return this;
       }
     };
   Component component =  renderer.
        getTableCellRendererComponent(
        header.getTable(), aColumn.getHeaderValue(), false, false, -1,
        columnIndex);
    rendererPane.add(component);
    rendererPane.paintComponent(g, component, header, cellRect.x, cellRect.y,
                                cellRect.width, cellRect.height, true);


  }
  /**
   * 在Graphics上画出列组的形状�?
   * @param g
   * @param cellRect
   * @param cGroup
   */
  private void paintCell(Graphics g, Rectangle cellRect, ColumnGroup cGroup) {
    TableCellRenderer renderer = cGroup.getHeaderRenderer();
    Component component = renderer.getTableCellRendererComponent(
      header.getTable(), cGroup.getHeaderValue(),false, false, -1, -1);
    rendererPane.add(component);
    rendererPane.paintComponent(g, component, header, cellRect.x, cellRect.y,
                                cellRect.width, cellRect.height, true);

  }
  private int getHeaderHeight() {
   int height = 0;
   TableColumnModel columnModel = header.getColumnModel();
   for(int column = 0; column < columnModel.getColumnCount(); column++) {
     TableColumn aColumn = columnModel.getColumn(column);
     TableCellRenderer renderer = aColumn.getHeaderRenderer();
     Component comp = new DefaultTableCellRenderer().getTableCellRendererComponent(
       header.getTable(), aColumn.getHeaderValue(), false, false,-1, column);
     int cHeight = comp.getPreferredSize().height;
     Enumeration enum1 = ((JTableHeaderEx)header).getColumnGroups(aColumn);
     if (enum1 != null) {
       while (enum1.hasMoreElements()) {
         ColumnGroup cGroup = (ColumnGroup)enum1.nextElement();
         cHeight += cGroup.getSize(header.getTable()).height;
       }
     }
     height = Math.max(height, cHeight);
   }
   return height;
 }

 private Dimension createHeaderSize(long width) {
   TableColumnModel columnModel = header.getColumnModel();
   width += columnModel.getColumnMargin() * columnModel.getColumnCount();
   if (width > Integer.MAX_VALUE) {
     width = Integer.MAX_VALUE;
   }
   return new Dimension((int)width, getHeaderHeight());
 }

 public Dimension getPreferredSize(JComponent c) {
   long width = 0;
   Enumeration enumeration = header.getColumnModel().getColumns();
   while (enumeration.hasMoreElements()) {
     TableColumn aColumn = (TableColumn)enumeration.nextElement();
     width = width + aColumn.getPreferredWidth();
   }
   return createHeaderSize(width);
 }
 /***********************************************************************/
// protected MouseInputListener createMouseInputListener() {
//   return new MouseInputHandler((JTableHeaderEx)header);
// }

// public class MouseInputHandler extends BasicTableHeaderUI.MouseInputHandler {
//   private Component dispatchComponent;
//   protected JTableHeaderEx header;
//
//   public MouseInputHandler(JTableHeaderEx header) {
//     this.header = header;
//   }
//
//   private void setDispatchComponent(MouseEvent e) {
//     Component editorComponent = header.getEditorComponent();
//     Point p = e.getPoint();
//     Point p2 = SwingUtilities.convertPoint(header, p, editorComponent);
//     dispatchComponent = SwingUtilities.getDeepestComponentAt(editorComponent,
//                                                              p2.x, p2.y);
//   }
//
//   private boolean repostEvent(MouseEvent e) {
//     if (dispatchComponent == null) {
//       return false;
//     }
//     MouseEvent e2 = SwingUtilities.convertMouseEvent(header, e, dispatchComponent);
//     dispatchComponent.dispatchEvent(e2);
//     return true;
//   }
//
//   public void mousePressed(MouseEvent e) {
//     if (!SwingUtilities.isLeftMouseButton(e)) {
//       return;
//     }
//     super.mousePressed(e);
//
//     if (header.getResizingColumn() == null) {
//       Point p = e.getPoint();
//       TableColumnModel columnModel = header.getColumnModel();
//       int index = columnModel.getColumnIndexAtX(p.x);
//       if (index != -1) {
//         if (header.editCellAt(index, e)) {
//           setDispatchComponent(e);
//           repostEvent(e);
//         }
//       }
//     }
//   }
//
//   public void mouseReleased(MouseEvent e) {
//     super.mouseReleased(e);
//     if (!SwingUtilities.isLeftMouseButton(e)) {
//       return;
//     }
//     repostEvent(e);
//     dispatchComponent = null;
//   }
//
// }

 /***********************************************************************/

}
